package com.arpa.oms.controller;


import cn.hutool.core.map.MapUtil;
import cn.hutool.http.server.HttpServerRequest;
import com.arpa.core.utils.collection.CollectionUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsUnpackRuleDTO;
import com.arpa.oms.domain.entity.OmsUnpackRule;
import com.arpa.oms.domain.vo.OmsUnpackRuleVO;
import com.arpa.oms.service.IOmsUnpackRuleService;
import com.arpa.wms.domain.consts.PropertyConst;
import com.arpa.wms.domain.enums.StatusEnum;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 拆单规则 前端控制器
 * </p>
 *
 * @author spb
 * @since 2021-07-23
 */
@Api(tags = "拆单规则管理")
@Log4j2(topic = "business")
@RestController
@RequestMapping(value = "/omsUnpackRule")
public class OmsUnpackRuleController {

    @Autowired
    private IOmsUnpackRuleService iOmsUnpackRuleService;

    @ApiOperation(value = "获取 拆单规则 列表", notes = "获取 拆单规则 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsUnpackRule.class))
    @NeedLogin
    @GetMapping
    public Result list(OmsUnpackRuleDTO unpackRuleDTO, HttpServletRequest request) {
        try {
            Page returnPage = new Page(unpackRuleDTO.getPageNum(), unpackRuleDTO.getPageSize());
            Integer count = iOmsUnpackRuleService.listCount(unpackRuleDTO);
            List<OmsUnpackRuleVO> list = iOmsUnpackRuleService.listPage(unpackRuleDTO);
            returnPage.setRecords(list);
            returnPage.setTotal(count);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "新增 拆单规则 列表", notes = "新增 拆单规则 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping
    public Result create(@RequestBody OmsUnpackRule unpackRule) {
        try {
            if (null != unpackRule) {
                iOmsUnpackRuleService.create(unpackRule);
            }
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "更新 拆单规则 列表", notes = "更新 拆单规则 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping
    public Result update(@RequestBody OmsUnpackRule unpackRule) {
        try {
            if (null != unpackRule) {
                boolean flag = iOmsUnpackRuleService.updateByCode(unpackRule);
                if (flag) {
                    return Result.ok("操作成功");
                } else {
                    return Result.error("操作失败");
                }
            }
            return Result.error("数据不可为空");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 启用拆单规则
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "启用 拆单规则", notes = "启用 拆单规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("active")
    public Result active(@RequestParam String code, HttpServerRequest request) {
        boolean flag = iOmsUnpackRuleService.active(code, StatusEnum.ACTIVE.getValue());
        return flag ? Result.ok() : Result.error();
    }

    /**
     * 停用拆单规则
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "停用 拆单规则", notes = "停用 拆单规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("unactive")
    public Result unactive(@RequestParam String code, HttpServerRequest request) {
        boolean flag = iOmsUnpackRuleService.unactive(code, StatusEnum.UNACTIVE.getValue());
        return flag ? Result.ok() : Result.error();
    }

    /**
     * 删除 拆单规则
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "停用 拆单规则", notes = "停用 拆单规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @DeleteMapping
    public Result delete(@RequestParam List<String> codes, HttpServerRequest request) {
        if (CollectionUtil.isEmpty(codes)) {
            return Result.error("未获取到需要删除的记录");
        }
        Map<String, Integer> result = iOmsUnpackRuleService.deleteByCodes(codes);
        Integer success = MapUtil.getInt(result, PropertyConst.SUCCESS);
        int failNum = codes.size() - success;
        return Result.ok("删除成功" + success + "条；失败" + failNum + "条；");
    }


    @ApiOperation(value = "获取 拆单规则 详情", notes = "获取 拆单规则 详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsUnpackRule.class))
    @NeedLogin
    @GetMapping("/detail/{code}")
    public Result getInfoByCode(@PathVariable(value = "code", required = true) String code) {
        try {
           OmsUnpackRule omsUnpackRule = iOmsUnpackRuleService.getDetailInfoByCode(code);
            return Result.ok().data(omsUnpackRule);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }
}
